<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人才类别" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择人才类别" clearable>
          <el-option label="创业导师" :value="0" />
          <el-option label="成果转化专员" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-select v-model="queryParams.education" placeholder="请选择学历" clearable>
          <el-option label="博士" value="博士" />
          <el-option label="硕士" value="硕士" />
          <el-option label="本科" value="本科" />
          <el-option label="专科" value="专科" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:talent:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:talent:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="talentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="头像" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <el-image 
            style="width: 50px; height: 50px"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="人才类别" align="center" prop="category">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.category === 0" type="success">教师</el-tag>
          <el-tag v-else type="warning">专家</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="领域" align="center" prop="field">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in scope.row.field" :key="index" style="margin-right: 5px">
            {{ item }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="能力" align="center" prop="ability">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in scope.row.ability" :key="index" type="success" style="margin-right: 5px">
            {{ item }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="所在地区" align="center">
        <template slot-scope="scope">
          {{ scope.row.province }} {{ scope.row.city }}
        </template>
      </el-table-column>
      <el-table-column label="学校" align="center" prop="school" />
      <el-table-column label="聘任日期" align="center" prop="hiredate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hiredate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:talent:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:talent:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人才对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.avatar" :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="人才类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择人才类别">
            <el-option label="创业导师" :value="0" />
            <el-option label="成果转化专员" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="领域" prop="field">
          <el-select
            v-model="form.field"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请输入领域，按回车确认">
          </el-select>
        </el-form-item>
        <el-form-item label="能力" prop="ability">
          <el-select
            v-model="form.ability"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请输入能力，按回车确认">
          </el-select>
        </el-form-item>
        <el-form-item label="所在地区" required>
          <el-col :span="11">
            <el-form-item prop="province">
              <el-input v-model="form.province" placeholder="省/区" />
            </el-form-item>
          </el-col>
          <el-col :span="2" class="text-center">
            <span class="demonstration">-</span>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="city">
              <el-input v-model="form.city" placeholder="市" />
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入学校" />
        </el-form-item>
        <el-form-item label="聘任日期" prop="hiredate">
          <el-date-picker
            v-model="form.hiredate"
            type="date"
            placeholder="选择聘任日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-select v-model="form.education" placeholder="请选择学历">
            <el-option label="博士" value="博士" />
            <el-option label="硕士" value="硕士" />
            <el-option label="本科" value="本科" />
            <el-option label="专科" value="专科" />
          </el-select>
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input
            v-model="form.introduction"
            type="textarea"
            placeholder="请输入介绍"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTalentPerson,
  getTalentPerson,
  delTalentPerson,
  addTalentPerson,
  updateTalentPerson
} from "@/api/system/talentPerson";
import { uploadFile } from "@/api/system/file";

export default {
  name: "TalentPerson",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人才库表格数据
      talentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上传地址
      uploadUrl: process.env.VUE_APP_BASE_API + "/file/upload",
      // 领域选项
      fieldOptions: [],
      // 能力选项
      abilityOptions: [],
      // 查询参数
      queryParams: {
        current: 1,
        pageSize: 10,
        name: null,
        category: null,
        education: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "人才类别不能为空", trigger: "change" }
        ],
        field: [
          { required: true, message: "领域不能为空", trigger: "change" }
        ],
        ability: [
          { required: true, message: "能力不能为空", trigger: "change" }
        ],
        province: [
          { required: true, message: "所在区不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "所在市不能为空", trigger: "blur" }
        ],
        school: [
          { required: true, message: "学校不能为空", trigger: "blur" }
        ],
        hiredate: [
          { required: true, message: "聘任日期不能为空", trigger: "blur" }
        ],
        education: [
          { required: true, message: "学历不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人才库列表 */
    getList() {
      this.loading = true;
      listTalentPerson(this.queryParams).then(response => {
        this.talentList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        avatar: null,
        category: null,
        field: [],
        ability: [],
        province: null,
        city: null,
        school: null,
        hiredate: null,
        education: null,
        introduction: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人才";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTalentPerson(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人才";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTalentPerson(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTalentPerson(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人才编号为"' + ids + '"的数据项？').then(function() {
        return delTalentPerson(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 头像上传成功处理
    handleAvatarSuccess(res, file) {
      if (res.code === 200) {
        this.form.avatar = res.data.url;
      } else {
        this.$message.error(res.msg || "上传失败");
      }
    },
    // 头像上传前的处理
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
